#!/bin/bash

set_gpio()
{
    local NET_NAME=$1
    local OUT_VAL=$2
    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
        echo "set_gpio: can not find gpio, $NET_NAME"
        return 1
    fi

    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
        echo " failed"
        return 1
    fi

    echo " success"
    return 0
}

get_gpio()
{
    local NET_NAME=$1
    local ret=2
    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
        echo "get_gpio: can not find gpio, $NET_NAME"
        return 2
    fi

    echo -n "get_gpio: get $NET_NAME"
    if ! ret=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"); then
        echo " failed"
        return 2
    fi

    echo " success"
    return "${ret}"
}

check_nuvoton_board()
{
    set +e
    grep nuvoton /sys/firmware/devicetree/base/compatible
    return 0
}

get_product_version()
{
    local FRU_NAME=$1
    local PRODUCT_VERSION
    PRODUCT_VERSION=$(busctl get-property xyz.openbmc_project.EntityManager /xyz/openbmc_project/inventory/system/board/Yosemite_4_"$FRU_NAME" xyz.openbmc_project.Inventory.Decorator.Revision Version| awk -F\" '{print $2}')
    echo "$PRODUCT_VERSION"
}

set_sentinel_dome_slot_present_percentage()
{
    local sentinel_dome_slot_present_percentage="$1"

    if ! busctl set-property "xyz.openbmc_project.VirtualSensor" \
            "/xyz/openbmc_project/sensors/utilization/SENTINEL_DOME_SLOT_PRESENT_PERCENTAGE" \
            "xyz.openbmc_project.Sensor.Value" Value d "$sentinel_dome_slot_present_percentage" 2>&1; then
        echo "Failed to set SENTINEL_DOME_SLOT_PRESENT_PERCENTAGE to ${sentinel_dome_slot_present_percentage}%."
        return 1
    else
        echo "Successfully set SENTINEL_DOME_SLOT_PRESENT_PERCENTAGE to ${sentinel_dome_slot_present_percentage}%."
        return 0
    fi
}

handle_service_window()
{
    local value=0
    local present_count=0
    local sentinel_dome_slot_present_percentage

    for i in {1..8}; do
        value=$(cat /tmp/gpio/PRSNT_SB_SLOT"${i}"_N)
        if [ "$value" -eq 0 ]; then
            present_count=$((present_count + 1))
        fi
    done

    echo "Slot present count: ${present_count}"
    sentinel_dome_slot_present_percentage=$(awk "BEGIN {printf \"%.1f\", $present_count * 12.5}")
    if ! set_sentinel_dome_slot_present_percentage "$sentinel_dome_slot_present_percentage"; then
        return 1
    else
        return 0
    fi
}
